package com.view.pages.worldtown
{
	import beherit.animation.MapEngine;
	import beherit.animation.Platform3DKeyboard;
	import beherit.animation.SimpleGravity;
	import beherit.animation.Static3DMouse;
	import beherit.components.pages.mx.Page3DDynamic;
	import beherit.pv3d.SimpleTreeArray;
	
	import caurina.transitions.Tweener;
	
	import com.view.pages.worldtown.objects.AsosTower;
	import com.view.pages.worldtown.objects.Birds;
	import com.view.pages.worldtown.objects.Shops;
	
	import flash.events.Event;
	
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.parsers.Collada;
	import org.papervision3d.objects.primitives.Sphere;
	//PROJECT RELATED

	public class World3D extends Page3DDynamic
	{
		//INTERACTION
		private var $im:Static3DMouse;
		private var $qk:Platform3DKeyboard;
		private var $sg:SimpleGravity;
		private var $me:MapEngine;
		
		//OBJECTS
		private var $env:Sphere;
		private var $treeArray:SimpleTreeArray;
		private var $binArray:SimpleTreeArray;
		private var $colladaArray:Shops;
		private var $asos:AsosTower;
		private var $birds:Birds;
		
		//ENVIROMENT
		private var $floor:DisplayObject3D;
		private var $world:DisplayObject3D;

		public function World3D ()
		{
			super ();
		}
		override protected function updateRender():void
		{	
			//$me.updateCache();
			$env.rotationY = $_camera.rotationY/4;
			$env.x = $_camera.x;
			$env.z = $_camera.z;
			
			$treeArray.update($_camera.rotationY);
			$binArray.update($_camera.rotationY);
			$birds.update($_camera);
			
			
			//$me.update();
			$im.render();
			$qk.render();
			$sg.render();
				
		}

		override protected function initControls():void
		{
			loadedSettings.stage = stage;
			loadedSettings.camera = $_camera;
			$_camera.z = 200;
			$_camera.y = 300 ;	
			
			//ENVIROMENT
			$world = new DisplayObject3D();
			$floor = new DisplayObject3D();
			
			//FLOOR
			var $field:Collada = new Collada(loadedXml.road);
			$field.x = 0;
			$field.y = 0;
			$floor.addChild($field);
			
			//SPHERE
			var $mat:BitmapMaterial = new BitmapMaterial(loadedImages.sky.bitmapData);
			$mat.oneSide = false;
			$env = new Sphere($mat,25000,20,20);
			$env.y = -2000;
			$floor.addChild($env);
			
			//ASOS BUILDING
			$asos = new AsosTower(loadedXml,loadedImages,loadedSettings);
			$asos.z = 9745;
			$world.addChild($asos);

			//TREES
			$treeArray = new SimpleTreeArray(loadedImages.tree,[[0,1240,5000],[0,1240,2100],[0,1240,-800],[0,1240,-3700],[0,1240,-6700]]);
			$world.addChild($treeArray);
			
			//BIN
			$binArray = new SimpleTreeArray(loadedImages.bin,[[3500,0,-9500],[-3500,0,-9500],[3500,0,9400],[-3500,0,9400],[3500,0,-1650],[-3500,0,1550]]);
			$binArray.y = 200;
			$world.addChild($binArray);
			
			//SHOPS
			$colladaArray = new Shops(loadedXml,loadedImages,[
			[3900,0,7782,0,"Room0"],
			[3900,0,3841,0,"Room0"],
			[3900,0,-100,0,"Room1"],
			[3900,0,-4041,0,"Room2"],
			[3900,0,-7982,0,"Room4"],
			[1971,0,-9950,90,"Room0"],
			[-1971,0,-9950,90,"Room0"],
			[-3860,0,-7982,180,"Room3"],
			[-3860,0,-4041,180,"Room5"],
			[-3860,0,-100,180,"Room6"],
			[-3860,0,3841,180,"Room0"],
			[-3860,0,7782,180,"Room0"]
			],loadedSettings);
			$colladaArray.addEventListener(Shops.DOORS_CLICKED_IN,onDoorsClickedIn);
			$colladaArray.addEventListener(Shops.DOORS_CLICKED_OUT,onDoorsClickedOut);
			$colladaArray.addEventListener(Shops.DOORS_CLOSED_IN,onDoorsClosedIn);
			$colladaArray.addEventListener(Shops.DOORS_CLOSED_OUT,onDoorsClosedOut);
			$colladaArray.$globalFloor = $floor;
			$colladaArray.start();
			$world.addChild($colladaArray);
			
			//BIRDS
			$birds = new Birds();
			$world.addChild($birds);
			
			
			$_scene.addChild($floor);
			$_scene.addChild($world);
			
			//INTERACTIVE MANAGERS
			$im = new Static3DMouse($_camera,loadedSettings);
			//$qk = new Platform3DKeyboard($_camera,loadedSettings);
			$qk = new Platform3DKeyboard($_camera,loadedSettings);
			$qk.speed = 100;
			$sg = new SimpleGravity($_camera,800,50);
			$me = new MapEngine($_camera);
			//$qk.addEventListener(Platform3DKeyboard.X_CHANGED,onXchanged);
			//$qk.addEventListener(Platform3DKeyboard.Z_CHANGED,onZchanged);
			//$me.x = 100;
			//$me.y = 100;
			
			addChild($me);
			$me.scaleX = .1;
			$me.scaleY = .1;
			
			startRender();
			
			
			
		}
		private function onDoorsClickedIn(_e:Event):void
		{
			showWorld();
			$treeArray.useOwnContainer = true;
			$binArray.useOwnContainer = true;
			Tweener.addTween($_camera,{x:_e.target.$tx,z:_e.target.$tz,time:2,transition:"easeInOutSine"});
		}
		private function onDoorsClickedOut(_e:Event):void
		{
			$treeArray.useOwnContainer = true;
			$binArray.useOwnContainer = true;
			Tweener.addTween($_camera,{x:_e.target.$tx,z:_e.target.$tz,time:2,transition:"easeInOutSine"});
		}
		private function onDoorsClosedOut(_e:Event):void
		{
			$treeArray.useOwnContainer = false;
			$binArray.useOwnContainer = false;
		}
		private function onDoorsClosedIn(_e:Event):void
		{
			$treeArray.useOwnContainer = false;
			$binArray.useOwnContainer = false;
			hideWorld();
		}
		private function hideWorld():void
		{
			$_scene.removeChild($floor);
			$world.removeChild($asos);
			$world.removeChild($treeArray);
		}
		private function showWorld():void
		{
			$_scene.addChild($floor);
			$world.addChild($asos);
			$world.addChild($treeArray);
		}
		override protected function initStart():void
		{
			trace("initialize");
		}
		override protected function onMouseLeave():void
		{
			if($im!=null){
				$im.focus = false;
			}
		}
		override protected function onMouseBack():void
		{
			if($im!=null){
				$im.focus = true;
			}
		}
	}
}